package com.ghc.action;

import java.util.ArrayList;
import java.util.List;
import com.ghc.dao.MailDao;
import com.ghc.pojo.Mail;
import com.ghc.service.MailServiceImpl;
import com.ghc.vo.MailVo;
import com.google.gson.Gson;

public class MailAction {
	
	private Mail mail = new Mail();
	private MailVo mailVo = new MailVo();
	public MailServiceImpl mailServiceImpl;
	private MailDao mailDao;
	private List<MailVo> mailList = new ArrayList<MailVo>();
	private String result;
	
	public String addMail(){
		try {
			mailDao.addMail(mail);
			result = "added";
		} catch (Exception e) {
			result = "fail";
		}
		return "success";
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public String showMail(){
		int allRow = mailDao.getRow();
		mailList = mailDao.showMail();
		List list = new ArrayList();
		list.add(allRow);
		list.add(mailList);
		
		setResult(new Gson().toJson(list));
		return "success";
	}
	
	public String delMail(){
		try {
			mailDao.delMail(mail.getId());
			result = "del";
		} catch (Exception e) {
			result = "fail";
		}
		return "success";
	}
	
	public String showMailOne(){
		mailVo = mailDao.showMailOne(mail.getId());
		result = new Gson().toJson(mailVo);
		return "success";
	}
	public String sendMail(){
		try {	
			mailServiceImpl.placeOrder(mailVo);
		    mailDao.updateMailStatus(mailVo.getId());
	        result = "sended";
		} catch (Exception e) {
			e.printStackTrace();
        	result = "fail";
		}
		 return "success";
	}
	
	public Mail getMail() {
		return mail;
	}
	public void setMail(Mail mail) {
		this.mail = mail;
	}
	public MailDao getMailDao() {
		return mailDao;
	}
	public void setMailDao(MailDao mailDao) {
		this.mailDao = mailDao;
	}
	public List<MailVo> getMailList() {
		return mailList;
	}
	public void setMailList(List<MailVo> mailList) {
		this.mailList = mailList;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public MailVo getMailVo() {
		return mailVo;
	}

	public void setMailVo(MailVo mailVo) {
		this.mailVo = mailVo;
	}

	public MailServiceImpl getMailServiceImpl() {
		return mailServiceImpl;
	}

	public void setMailServiceImpl(MailServiceImpl mailServiceImpl) {
		this.mailServiceImpl = mailServiceImpl;
	}
}
